/*
 * BaseApplicationTableModel.java
 */
package co.edu.uniandes.intercon.client.model;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.omg.CORBA.SystemException;

import co.edu.uniandes.intercon.core.model.BaseDto;

/**
 * <code>TableModel</code> used to represent the <code>ApplicationDto</code> structure in
 * a tabular way
 * 
 * @author Fabian Gutierrez [fagossa@gmail.com]
 */
public abstract class BaseP2PApplicationTableModel extends AbstractTableModel
		implements IBaseTableModel<BaseDto> {

	/**
	 * Serial version of the class
	 */
	private static final long serialVersionUID = -3921339699407441526L;
	
	/**
	 * Table data
	 */
	private List<BaseDto> data;
	
	/**
	 * Initializes the model
	 */
	public BaseP2PApplicationTableModel() throws SystemException {
		data = new ArrayList<BaseDto>();
	}

	/**
	 * @see AbstractTableModel#getRowCount
	 */
	public int getRowCount() {
		return data.size();
	}

	/**
	 * Retrieves the value of a specified cell
	 * 
	 * @param rowIndex
	 *            cell row where the data in going to be set
	 * @param columnIndex
	 *            cell column where the data in going to be set
	 * 
	 * @see AbstractTableModel#getValueAt
	 */
	public Object getValueAt(int rowIndex, int columnIndex) {
		Object value = null;
		BaseDto room = data.get(rowIndex);
		if (room != null) {
			value = getValueAt(columnIndex, room);
		}
		return value;
	}
	
	/**
	 * Retrieves a column from the specified object
	 * @param	columnIndex	cell column where the data in going to be set
	 * @param	dto	
	 * @return	Value of the column specified
	 */
	public abstract Object getValueAt(int columnIndex, BaseDto dto);

	/**
	 * Sets the records to be show in the table
	 * 
	 * @param records
	 *            collection of elements of <code>RoomDto</code> to be added to
	 *            the table
	 */
	public void setRecords(List<BaseDto> records) {
		data.clear();

		if (records == null) {
			return;
		}
		for (BaseDto room : records) {
			int pos = data.size();
			data.add(pos, room);
		}

		fireTableDataChanged();
	}

	/**
	 * @see suncertify.gui.core.BaseTableModel#getRecord(int)
	 */
	public BaseDto getRecord(int row) {
		assert row >= 0;
		return data.get(row);
	}

	/**
	 * @see suncertify.gui.core.BaseTableModel#setRecord(int, E)
	 */
	public void setRecord(int row, BaseDto dto) {
		assert row >= 0;
		assert dto != null;

		if(row==0) {
			data.add(dto);
			fireTableDataChanged();
		} else {
			data.set(row, dto);
			fireTableRowsUpdated(row, row);
		}
	}
	
	/**
	 * @see suncertify.gui.core.BaseTableModel#addRecord(E)
	 */
	public void addRecord(BaseDto dto) {
		assert dto != null;

		data.add(dto);
		fireTableDataChanged();
	}

	/**
	 * @see suncertify.gui.core.BaseTableModel#clearTable()
	 */
	public void clearTable() {
		data.clear();
		fireTableDataChanged();
	}
}
